/**
 * Created by Administrator on 15-10-9.
 */
var map;
function pi(){
    return  3.1415926535898;
}
pow=Math.pow;
sin=Math.sin;
cos=Math.cos;
asin=Math.asin;
min=Math.min;
sqrt=Math.sqrt;
window.onload=function(){
    
    getLocation_xianlu();
    
}

function setLocation_xianlu(lat,lng){
    var storage = window.localStorage;
    storage.setItem("lng", lng);
    storage.setItem("lat", lat);
    
    var id= $.getUrlParam("id");
    var url = getUrl();
    $.jsonp({
            url: 'http://'+url+'/api/mapline/detail-'+id,
            data: {},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
            //            $("#cover").attr('src',data.cover);
            $("#luxian").text(data.title);
            var complied_tpl = juicer($("#xl_tpl").text());
            var html = complied_tpl.render(data);
            $(".ui-map").append(html);
            //  百度地图API功能
            map = new BMap.Map("allmap");    // 创建Map实例
            var cPoint = new BMap.Point(data.goLongitude, data.goDimensions);//设置中心点坐标
            map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
            map.centerAndZoom(cPoint,14);        //地图级别
            
            //GPS定位
            //            var geolocation = new BMap.Geolocation();
            //            geolocation.getCurrentPosition(function(r){
            var mPoint = new BMap.Point(lng, lat);
            if(true){
            //我的位置
            var mk = new BMap.Marker(mPoint);  // 创建标注
            map.addOverlay(mk);              // 将标注添加到地图中
            map.panTo(mPoint);
            
            //路线
            var p1 = new BMap.Point(data.goLongitude,data.goDimensions);
            var p2 = new BMap.Point(data.toLongitude,data.toDimensions);
            var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
            driving.search(p1, p2);
            
            
            //查找周边商铺
            $.jsonp({
                    url: 'http://'+url+'/api/supplier/shopsList?lnglat='+lng+','+lat,
                    data: {},
                    callbackParameter: "callback",
                    success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                           // 创建地址解析器实例
                           var myGeo = new BMap.Geocoder();
                           // 将地址解析结果显示在地图上,并调整地图视野
                           myGeo.getPoint(ele.addr+ele.addrInfo+ele.address , function(point){
                                          earthRadius = 6371;
                                          var lat0 = (lat * pi()) / 180;
                                          var lng0 = (lng * pi()) / 180;
                                          var lat1 = (point.lat * pi()) / 180;
                                          var lng1 = (point.lng * pi()) / 180;
                                          calcLongitude = lng1 - lng0;
                                          calcLatitude = lat1 - lat0;
                                          stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
                                          stepTwo = 2 * asin(min(1, sqrt(stepOne)));
                                          calculatedDistance = earthRadius * stepTwo;
                                          ele.distance=calculatedDistance.toFixed(2);
                                          
                                          if (point) {
                                          var spIcon = new BMap.Icon("img/jpg02.png", new BMap.Size(40,64));
                                          var marker2 = new BMap.Marker(point, {icon: spIcon});
                                          var infoWindow = new BMap.InfoWindow(
                                                                               '<div class="ui-user-follow-info clearfix">'+
                                                                               '<div class="ui-user-avatar">'+
                                                                               '<img src='+ele.cover[0]+' width="55" height="55" alt=""></div>'+
                                                                               '<div class="ui-user-intro">'+
                                                                               '<p>'+ele.shopName+'</p>'+
                                                                               '<p class="ui-txt-muted"><i class="icons-lbs"></i>距离'+ele.distance+'KM</p>'+
                                                                               '</div></div>'
                                                                               );  // 创建信息窗口对象
                                          marker2.addEventListener("click", function(){marker2.openInfoWindow(infoWindow);});
                                          map.addOverlay(marker2);
                                          }else{
                                          //alert("找不到您选择的地址!");
                                          }
                                          }, ele.attr);
                           });
                    },
                    error: function (xOptions, textStatus) {
                    }
                    });
            
            ////周边P停车位、WC、加油站、医院
            //function addMarker(results,point, index){
            //    // 创建P停车场的标注图标
            //    var tccIcon = new BMap.Icon("img/jpg03.png",   //图片地址
            //        new BMap.Size(40, 64),                // 标注显示大小
            //        {
            //            offset: new BMap.Size(20, 64),     // 标注底部小尖尖的偏移量
            //            imageOffset: new BMap.Size(0, 0)   // 这里相当于CSS sprites
            //        });
            //    // 创建厕所的标注图标
            //    var csIcon = new BMap.Icon("img/jpg07.png",   //图片地址
            //        new BMap.Size(40, 64),                // 标注显示大小
            //        {
            //            offset: new BMap.Size(20, 64),     // 标注底部小尖尖的偏移量
            //            imageOffset: new BMap.Size(0, 0)   // 这里相当于CSS sprites
            //        });
            //    // 创建加油站的标注图标
            //    var jyIcon = new BMap.Icon("img/jpg04.png",   //图片地址
            //        new BMap.Size(40, 64),                // 标注显示大小
            //        {
            //            offset: new BMap.Size(20, 64),     // 标注底部小尖尖的偏移量
            //            imageOffset: new BMap.Size(0, 0)   // 这里相当于CSS sprites
            //        });
            //    // 创建医院的标注图标
            //    var yyIcon = new BMap.Icon("img/jpg05.png",   //图片地址
            //        new BMap.Size(40, 64),                // 标注显示大小
            //        {
            //            offset: new BMap.Size(20, 64),     // 标注底部小尖尖的偏移量
            //            imageOffset: new BMap.Size(0, -64)   // 这里相当于CSS sprites
            //        });
            //    var fwIcon = "";
            //    if(results.keyword == "P停车位"){
            //        fwIcon = tccIcon;
            //    }
            //    if(results.keyword == "厕所"){
            //        fwIcon = csIcon;
            //    }
            //    if(results.keyword == "加油站"){
            //        fwIcon = jyIcon;
            //    }
            //    if(results.keyword == "医院"){
            //        fwIcon = yyIcon;
            //    }
            //    var marker = new BMap.Marker(point.point, {icon: fwIcon});
            //    var infoWindow = new BMap.InfoWindow("<div style='line-height:1.8em;font-size:12px'><b>名称:</b>"+point.title+"</br><b>地址:</b>"+point.address+"</br><b>电话:</b>"+point.phoneNumber+"</br><b>口碑：</b><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><img src='http://cdn2.iconfinder.com/data/icons/diagona/icon/16/031.png' /><a style='text-decoration:none;color:#2679BA;float:right' target='_blank' href='http://www.ui-love.com'>详情>></a></div>");  // 创建信息窗口对象
            //    marker.addEventListener("click", function(){marker.openInfoWindow(infoWindow);});//提示信息
            //    map.addOverlay(marker);
            //}
            //var searchComplete = function (results){
            //    //map.clearOverlays();
            //    if (local.getStatus() != BMAP_STATUS_SUCCESS){
            //        return ;
            //    }
            //    for(var i=0;i<4;i++){
            //        //var res = results[i];
            //        for(var cnt = 0; cnt < results.getCurrentNumPois(); cnt++){
            //            var point = results.getPoi(cnt);
            //            addMarker(results,point, cnt);
            //        }
            //    }
            //}
            ////var circle = new BMap.Circle(mPoint,5000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity:0.2, strokeOpacity: 0.1});
            ////map.addOverlay(circle);
            //var local = new BMap.LocalSearch(map,{onSearchComplete: searchComplete});   //构造一个查询
            //local.searchNearby("P停车位",mPoint,5000);
            //local.searchNearby('厕所',mPoint,5000);
            //local.searchNearby('加油站',mPoint,5000);
            //local.searchNearby('医院',mPoint,5000);
            
            
            }
            else {
            //alert('failed'+this.getStatus());
            }
            //            },{enableHighAccuracy: true});
            
            
            //向地图中添加缩放控件
            var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
            map.addControl(ctrl_nav);
            //console.log(data)
            
            if(data.maplineDesc.length>18){
            data.maplineDesc=data.maplineDesc.substring(0,18)+"......";
            }
            if(data.mapFlavor.length>18){
            data.mapFlavor=data.mapFlavor.substring(0,18)+"......";
            }
            if(data.travelPlan.length>18){
            data.travelPlan=data.travelPlan.substring(0,18)+"......";
            }
            if(data.recommendTravel.length>18){
            data.recommendTravel=data.recommendTravel.substring(0,18)+"......";
            }
            var complied_tpl = juicer($("#rec").text());
            var html = complied_tpl.render(data);
            $(".ui-map-road").append(html);
            },
            error: function (xOptions, textStatus) {
            
            }
            });
    
}